package cn.iocoder.yudao.module.cmt.controller.admin.content;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.content.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.content.ContentArticleDO;
import cn.iocoder.yudao.module.cmt.service.content.ContentArticleService;

@Tag(name = "管理后台 - 文章")
@RestController
@RequestMapping("/cmt/content-article")
@Validated
public class ContentArticleController {

    @Resource
    private ContentArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建文章")
    @PreAuthorize("@ss.hasPermission('cmt:content-article:create')")
    public CommonResult<Long> createArticle(@Valid @RequestBody ContentArticleSaveReqVO createReqVO) {
        return success(articleService.createArticle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章")
    @PreAuthorize("@ss.hasPermission('cmt:content-article:update')")
    public CommonResult<Boolean> updateArticle(@Valid @RequestBody ContentArticleSaveReqVO updateReqVO) {
        articleService.updateArticle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:content-article:delete')")
    public CommonResult<Boolean> deleteArticle(@RequestParam("id") Long id) {
        articleService.deleteArticle(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除文章")
    @PreAuthorize("@ss.hasPermission('cmt:content-article:delete')")
    public CommonResult<Boolean> deleteArticleList(@RequestParam("ids") List<Long> ids) {
        articleService.deleteArticleListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:content-article:query')")
    public CommonResult<ContentArticleRespVO> getArticle(@RequestParam("id") Long id) {
        ContentArticleDO article = articleService.getArticle(id);
        return success(BeanUtils.toBean(article, ContentArticleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章分页")
    @PreAuthorize("@ss.hasPermission('cmt:content-article:query')")
    public CommonResult<PageResult<ContentArticleRespVO>> getArticlePage(@Valid ContentArticlePageReqVO pageReqVO) {
        PageResult<ContentArticleDO> pageResult = articleService.getArticlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContentArticleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:content-article:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleExcel(@Valid ContentArticlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContentArticleDO> list = articleService.getArticlePage(pageReqVO).getList();
        ExcelUtils.write(response, "文章.xls", "数据", ContentArticleRespVO.class,
                        BeanUtils.toBean(list, ContentArticleRespVO.class));
    }

}
